<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary"
                     @click="openAddCateDialog">
            添加分类
          </el-button>
        </el-col>
      </el-row>
      <!-- 下方表格 -->
      <tree-table class="treetable"
                  :data="cateList"
                  :columns="columns"
                  :selection-type="false"
                  :expand-type="false"
                  show-index
                  :border="true"
                  :show-row-hover="false">
        <template slot="isok"
                  slot-scope="scope">
          <i class="el-icon-success"
             style="color:lightgreen;"
             v-if="!scope.row.cat_deleted"></i>
          <i class="el-icon-error"
             style="color:red;"
             v-else></i>
        </template>
        <template slot="sort"
                  slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success"
                  v-else-if="scope.row.cat_level === 1">
            二级
          </el-tag>
          <el-tag type="warning"
                  v-else>
            三级
          </el-tag>
        </template>
        <template slot="opt">
          <el-button type="primary"
                     icon="el-icon-edit"
                     size="mini">
            编辑
          </el-button>
          <el-button type="danger"
                     icon="el-icon-delete"
                     size="mini">
            删除
          </el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum"
                     :page-sizes="[3, 5, 10, 15]"
                     :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total"></el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类"
               :visible.sync="addCatedialogVisible"
               width="50%"
               @close="addCateDialogClose">
      <el-form :rules="addCateFormRules"
               ref="addCateFormRef"
               :model="addCateInfo"
               label-width="100px">
        <el-form-item label="分类名称："
                      prop="cat_name">
          <el-input v-model="addCateInfo.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader v-model="addValue"
                       :options="parentCate"
                       :props="{
              expandTrigger: 'hover',
              value: 'cat_id',
              label: 'cat_name',
              children: 'children',
              checkStrictly: true
            }"
                       @change="parentCateChange"
                       clearable></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addCatedialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addCate">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.getCatesList()
  },
  data() {
    return {
      cateList: [],
      //查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0, //总数据
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '排序', type: 'template', template: 'sort' },
        { label: '操作', type: 'template', template: 'opt' }
      ],
      addCatedialogVisible: false,
      addCateInfo: {
        cat_name: '',
        //分类的父id
        cat_pid: 0,
        //分类的等级
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: { required: true, message: '请输入分类名', trigger: 'blur' }
      },
      parentCate: [],
      addValue: []
    }
  },
  methods: {
    //获取商品分类列表
    async getCatesList() {
      //发送获取商品分类列表的网络请求
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })

      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return
      }

      //将获取到的数据保存到本地
      // console.log(res)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getCatesList()
    },
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage
      this.getCatesList()
    },
    //打开添加分类对话框
    openAddCateDialog() {
      this.getParentCate()
      this.addCatedialogVisible = true
    },
    //请求所有父级分类
    async getParentCate() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })

      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return
      }

      this.parentCate = res.data
      // console.log(this.parentCate)
    },
    //联级选择器发生改变回调
    parentCateChange() {
      if (this.addValue.length > 0) {
        this.addCateInfo.cat_pid = this.addValue[this.addValue.length - 1]
        this.addCateInfo.cat_level = this.addValue.length
        // console.log(this.addCateInfo)
      } else {
        this.addCateInfo.cat_pid = 0
        this.addCateInfo.cat_level = 0
      }
    },
    //添加分类请求
    addCate() {
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) {
        } else {
          const { data: res } = await this.$http.post(
            'categories',
            this.addCateInfo
          )
          // console.log(this.addCateInfo)
          // console.log(res)
          if (res.meta.status !== 201) {
            this.$message.error(res.meta.msg)
            return
          }
          this.$message.success('添加分类成功！')
          this.getCatesList()
          this.addCatedialogVisible = false
        }
      })
    },
    //关闭对话框
    addCateDialogClose() {
      this.$refs.addCateFormRef.resetFields()
      this.addCateInfo = {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      }
      this.addValue = []
    }
  }
}
</script>
<style lang="less" scoped>
.el-radio-group {
  margin-left: 30px;
}
.treetable {
  margin-top: 30px;
}
.el-cascader {
  width: 100%;
}
</style>
